package com.practice.communication.aio;

import java.util.Scanner;

public class Client {
	
	private static String CLIENT_DEFAULT_IP = "127.0.0.1";
	
	private static int CLIENT_DEFAULT_PORT = 8080;
	
	private static ClientAsyncHandler clientHandler;
	
	public static void start() {
		start(CLIENT_DEFAULT_IP, CLIENT_DEFAULT_PORT);
	}
	
	public synchronized static void start(String ip, int port) {
		if (clientHandler != null) return;
		
		clientHandler = new ClientAsyncHandler(ip, port);
		new Thread(clientHandler, "Client").start();
	}
	
	public static boolean sendMsg(String msg) {
		if ("q".equals(msg)) return false;
		
		clientHandler.sendMsg(msg);
		return true;
	}
	
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		Client.start();
		System.out.println("请输入请求的信息：");
		Scanner scanner = new Scanner(System.in);
		while (Client.sendMsg(scanner.nextLine()));
	}

}
